package com.zl.exam.controller;

import com.zl.exam.pojo.Companyinfo;
import com.zl.exam.pojo.Orderinfo;
import com.zl.exam.pojo.Typeinfo;
import com.zl.exam.service.CompanyinfoService;
import com.zl.exam.service.OrderinfoService;
import com.zl.exam.service.TypeinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class MyController {


    @Autowired
    private OrderinfoService orderinfoService;

    @Autowired
    private CompanyinfoService companyinfoService;

    @Autowired
    private TypeinfoService typeinfoService;


    @GetMapping({"/","toLogin"})
    public String toLogin(){
        return "login";
    }

    @PostMapping("/login")
    public String login(String name,String pwd,Model model){
        if (!name.equals("zyf")){
            model.addAttribute("message","用户名不正确");
            return "login";
        }
        if (!pwd.equals("123")){
            model.addAttribute("message","密码不正确");
            return "login";
        }
        return "redirect:/findAll";
    }

    @GetMapping("/findAll")
    public String findAll(Model model){
        List<Orderinfo> list = orderinfoService.findAll();
        model.addAttribute("list",list);
        return "main";
    }

    @GetMapping("/order/{oid}")
    public String deleteById(@PathVariable("oid") Integer oid){
        orderinfoService.removeById(oid);
        return "redirect:/findAll";
    }

    @GetMapping("/goInsert")
    public String toInsert(Model model){
        List<Typeinfo> typeinfoList = typeinfoService.list(null);
        List<Companyinfo> companyinfoList = companyinfoService.list(null);
        model.addAttribute("typeinfoList",typeinfoList);
        model.addAttribute("companyinfoList",companyinfoList);
        return "add";
    }

    @PostMapping("/order")
    public String insert(Orderinfo orderinfo){
        Orderinfo o=new Orderinfo();
        orderinfoService.save(orderinfo);
        return "redirect:/findAll";

    }
}
